package com.ggh.jinjilive.views.live.v2;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.TextureView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.ggh.jinjilive.ui.live.AudienceLiveActivity;
import com.tencent.live2.V2TXLiveDef;
import com.tencent.live2.V2TXLivePlayer;
import com.tencent.live2.V2TXLivePlayerObserver;
import com.tencent.live2.impl.V2TXLivePlayerImpl;
import com.tencent.rtmp.ui.TXCloudVideoView;


public class V2PULLLiveRoomImpl extends V2PULLLiveRoom {
    protected static V2PULLLiveRoomImpl mInstance = null;

    //直播拉流的视频播放器
    protected V2TXLivePlayer mLivePlayer;



    public static V2PULLLiveRoom sharedInstance(Context context) {
        if (mInstance == null) {
            synchronized (V2PULLLiveRoomImpl.class) {
                if (mInstance == null) {
                    mInstance = new V2PULLLiveRoomImpl(context);
                }
            }
        }
        return mInstance;
    }



    protected V2PULLLiveRoomImpl(Context context) {
        //直播拉流的视频播放器
        mLivePlayer = new V2TXLivePlayerImpl(context);
        //设置播放器回调。通过设置回调，可以监听 V2TXLivePlayer 播放器的一些回调事件，包括播放器状态、播放音量回调、音视频首帧回调、统计数据、警告和错误信息等。
        mLivePlayer.setObserver(new V2TXLivePlayerObserver() {
            @Override
            public void onError(V2TXLivePlayer v2TXLivePlayer, int i, String s, Bundle bundle) {
                super.onError(v2TXLivePlayer, i, s, bundle);
                LogUtils.d("直播播放器错误通知，播放器出现错误时，会回调该通知。");
                ActivityUtils.finishActivity(AudienceLiveActivity.class);
            }
        });
    }



    @Override
    public void setObserver() {
        mLivePlayer.setObserver(null);
    }

    @Override
    public int setRenderView(TXCloudVideoView view) {
        //设置播放器渲染 View，该控件负责显示视频内容。
        return mLivePlayer.setRenderView(view);
    }

    @Override
    public int setRenderView(SurfaceView view) {
        //设置播放器渲染 View，该控件负责显示视频内容。
        return mLivePlayer.setRenderView(view);
    }

    @Override
    public int setRenderView(TextureView view) {
        return mLivePlayer.setRenderView(view);
    }

    @Override
    public int startPlay(String url) {
        return mLivePlayer.startPlay(url);
    }

    @Override
    public int stopPlay() {
        return mLivePlayer.stopPlay();
    }

    @Override
    public int isPlaying() {
        return mLivePlayer.isPlaying();
    }

    @Override
    public int setRenderRotation(V2TXLiveDef.V2TXLiveRotation rotation) {
        return mLivePlayer.setRenderRotation(rotation);
    }

    @Override
    public int setRenderFillMode(V2TXLiveDef.V2TXLiveFillMode mode) {
        return mLivePlayer.setRenderFillMode(mode);
    }

    @Override
    public int pauseVideo() {
        return mLivePlayer.pauseVideo();
    }

    @Override
    public int resumeVideo() {
        return mLivePlayer.resumeVideo();
    }

    @Override
    public int snapshot() {
        return mLivePlayer.snapshot();
    }

    @Override
    public int enableCustomRendering(boolean enable, V2TXLiveDef.V2TXLivePixelFormat pixelFormat, V2TXLiveDef.V2TXLiveBufferType bufferType) {
        return mLivePlayer.enableCustomRendering(enable,pixelFormat,bufferType);
    }

    @Override
    public int pauseAudio() {
        return mLivePlayer.pauseAudio();
    }

    @Override
    public int resumeAudio() {
        return mLivePlayer.resumeAudio();
    }

    @Override
    public int setPlayoutVolume(int volume) {
        return mLivePlayer.setPlayoutVolume(volume);
    }

    @Override
    public int enableVolumeEvaluation(int intervalMs) {
        return mLivePlayer.enableVolumeEvaluation(intervalMs);
    }

    @Override
    public int setCacheParams(float minTime, float maxTime) {
        return mLivePlayer.setCacheParams(minTime,maxTime);
    }

    @Override
    public void showDebugView(boolean isShow) {
        mLivePlayer.showDebugView(isShow);
    }

}
